<script setup lang="ts">
const dataValue = ref(0);
const dataName = ref('');
const props = defineProps({
    modelValue: {
        type: Number,
        default: 0,
    },
    disabled: {
        type: Boolean,
        default: false
    },
    size:{
        type: String,
        default: 'default',
    }
});

//翻译汉字
const numToChinese = (num :number) =>{
        if(isNaN(num))return "";
        var strPrefix="";
        if(num < 0)strPrefix ="(负)";
        num=Math.abs(num);
        if(num>=1000000000000)return "";
        var strOutput = "";
        var strUnit = '仟佰拾亿仟佰拾万仟佰拾元角分';
        var strCapDgt='零壹贰叁肆伍陆柒捌玖';
        //将单位转换为分
        let strNum = num + "00";
        var intPos = strNum.indexOf('.');
        if (intPos >= 0){
            strNum = strNum.substring(0, intPos) + strNum.substr(intPos + 1, 2);
        }
        strUnit = strUnit.substr(strUnit.length - strNum.length);
        for (var i = 0; i < strNum.length; i++){
            strOutput += strCapDgt.substr(strNum.substr(i,1),1) + strUnit.substr(i,1);
        }
        return strPrefix+strOutput.replace(/零角零分$/, '整').replace(/零[仟佰拾]/g, '零').replace(/零{2,}/g, '零').replace(/零([亿|万])/g, '$1').replace(/零+元/, '元').replace(/亿零{0,3}万/, '亿').replace(/^元/, "零元");
}

const emit = defineEmits(['update:modelValue']);

//修改参数
const change = (e:any) => {
    dataValue.value = e;
    dataName.value = numToChinese(dataValue.value);
    emit('update:modelValue', e)
}

onMounted(()=>{
    setTimeout(()=>{
        if(props.modelValue){
            dataValue.value = props.modelValue;
            dataName.value = numToChinese(dataValue.value);
        }
    },1000)
})

</script>
<template>
    <el-input-number v-model="dataValue" :size="props.size" :precision="2" :min="0" @change="change" label="请输入金额"/>
    <h5>{{ dataName }}</h5>
</template>
